package com.shiningstars.service;

import java.io.IOException;
import java.util.Arrays;
import java.util.Calendar;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.os.Looper;

import com.parse.ParseException;
import com.parse.ParseInstallation;
import com.shiningstars.general.Config;
import com.shiningstars.general.Constants;
import com.shiningstars.general.Messages;
import com.shiningstars.general.Utilities;
import com.shiningstars.php.WebHelper;
import com.shiningstars.tools.ExceptionHandler;

public class ParseService extends Service implements Constants, Config, Messages {

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {

		Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));

		final WebHelper webHelper = new WebHelper();

		new Thread() {
			@Override
			public void run() {

				Looper.prepare();

				Calendar c = Calendar.getInstance();
				int year = c.get(Calendar.YEAR);
				int month = c.get(Calendar.MONTH);
				int date = c.get(Calendar.DATE);

				String updateDate = Utilities.getDate(date, month, year);

				try {
					String mobileNumber = Utilities.getSharedPreference(getApplicationContext(), MOBILE_NUMBER);
					String imeiNumber = Utilities.getSharedPreference(getApplicationContext(), IMEI_NUMBER);
					String name = Utilities.getSharedPreference(getApplicationContext(), NAME);
					String role = Utilities.getSharedPreference(getApplicationContext(), ROLE);
					String centerName = Utilities.getSharedPreference(getApplicationContext(), CENTER);

					try {
						ParseInstallation.getCurrentInstallation().refresh();
						ParseInstallation.getCurrentInstallation().put(NAME, name);
						ParseInstallation.getCurrentInstallation().put(MOBILE_NUMBER, mobileNumber);
						ParseInstallation.getCurrentInstallation().put(IMEI_NUMBER, imeiNumber);
						ParseInstallation.getCurrentInstallation().put(CHANNELS, Arrays.asList(role));
						ParseInstallation.getCurrentInstallation().put(BUILD_VERSION, Build.VERSION.RELEASE);
						ParseInstallation.getCurrentInstallation().put(UPDATE_DATE, updateDate);

						if (!Utilities.isBlankString(centerName)) {
							ParseInstallation.getCurrentInstallation().put(CENTER_NAME, centerName);
						}

						ParseInstallation.getCurrentInstallation().saveInBackground();
					} catch (ParseException e) {
						if (e.getMessage().contains(OBJECT_NOT_FOUND)) {

							String objectId = ParseInstallation.getCurrentInstallation().getObjectId();
							String installationId = ParseInstallation.getCurrentInstallation().getInstallationId();

							JSONObject json = webHelper.getNewParseDetails(objectId, installationId,
									getApplicationContext());
							if (null != json && json.getBoolean(SUCCESS)) {
								ParseInstallation.getCurrentInstallation().setObjectId(json.getString(OBJECT_ID));
								ParseInstallation.getCurrentInstallation().put(NAME, name);
								ParseInstallation.getCurrentInstallation().put(MOBILE_NUMBER, mobileNumber);
								ParseInstallation.getCurrentInstallation().put(IMEI_NUMBER, imeiNumber);
								ParseInstallation.getCurrentInstallation().put(CHANNELS, Arrays.asList(role));
								ParseInstallation.getCurrentInstallation().put(BUILD_VERSION, Build.VERSION.RELEASE);
								ParseInstallation.getCurrentInstallation().put(UPDATE_DATE, updateDate);

								if (!Utilities.isBlankString(centerName)) {
									ParseInstallation.getCurrentInstallation().put(CENTER_NAME, centerName);
								}

								ParseInstallation.getCurrentInstallation().saveInBackground();

								webHelper.updateParseDetails(objectId, installationId, getApplicationContext());
							} else {
								webHelper.insertParseDetails(objectId, installationId, getApplicationContext());
							}
						}
					}
				} catch (IllegalArgumentException e) {
					Utilities.logError(getApplicationContext(), ParseService.class.getName(), e);
				} catch (JSONException e) {
					Utilities.logError(getApplicationContext(), ParseService.class.getName(), e);
				} catch (IOException e) {
				}

				stopService(new Intent(getApplicationContext(), ParseService.class));

				Looper.loop();
				Looper.myLooper().quit();
			}
		}.start();

		return START_NOT_STICKY;
	}
}